/**
 * @author: Pegasus  Date: 2018/5/17 Time: 17:04
 *
 * 极光推送
 */

import {
  Platform
} from 'react-native';
import JPushModule from 'jpush-react-native';

const receiveCustomMsgEvent = 'receivePushMsg';
const receiveNotificationEvent = 'receiveNotification';
const openNotificationEvent = 'openNotification';
const getRegistrationIdEvent = 'getRegistrationId';


// /**
//  *
//  */
// const JPushOnGetRegistrationId = () => {
//   JPushModule.getRegistrationID(registrationId => {
//     this.setState({
//       registrationId: registrationId
//     })
//   })
// };

/**
 * 初始化并监听，添加在主页componentDidMount中
 * @param receiveMsgCallback 通知 推送消息回调函数
 * @param receiveCustomMsgCallback 消息 自定义消息回调
 * @param openNotificationCallback 点击推送消息回调
 */
export const JPushOnListening = (receiveMsgCallback, receiveCustomMsgCallback, openNotificationCallback) => {
  if (Platform.OS === 'android') {
    JPushModule.initPush();
    JPushModule.getInfo(map => {

    });
    JPushModule.notifyJSDidLoad(resultCode => {
      console.log('resultCode: ' + resultCode)
      if (resultCode === 0) {
      }
    })
  } else {
    JPushModule.setupPush()
  }

  // 收到自定义的消息
  JPushModule.addReceiveCustomMsgListener(map => {
    // this.setState({
    //   pushMsg: map.message
    // });
    // console.log('extras: ' + map.extras);
    receiveCustomMsgCallback(map);
  });

  // 收到推送消息
  JPushModule.addReceiveNotificationListener(map => {
    // console.log('alertContent: ' + map.alertContent);
    // console.log('extras: ' + map.extras);
    // alert('alertContent: ' + map.alertContent);
    // var extra = JSON.parse(map.extras);
    // console.log(extra.key + ": " + extra.value);
    receiveMsgCallback(map);
  });

  // 用户点击通知
  JPushModule.addReceiveOpenNotificationListener(map => {
    // console.log('Opening notification!');
    // console.log('map.extra: ' + map.extras);
    // this.jumpSecondActivity()
    // JPushModule.jumpToPushActivity("SecondActivity");
    openNotificationCallback(map);
  });

  JPushModule.addGetRegistrationIdListener(registrationId => {
    console.log('Device register succeed, registrationId ' + registrationId)
    // alert('Device register succeed, registrationId ' + registrationId);
  })
};

/**
 *
 * @constructor
 */
export const JPushUnmount = () => {
  JPushModule.removeReceiveCustomMsgListener(receiveCustomMsgEvent);
  JPushModule.removeReceiveNotificationListener(receiveNotificationEvent);
  JPushModule.removeReceiveOpenNotificationListener(openNotificationEvent);
  JPushModule.removeGetRegistrationIdListener(getRegistrationIdEvent);
  console.log('Will clear all notifications');
  JPushModule.clearAllNotifications();
};

/**
 * 设置Tag
 * @constructor
 */
export const JPushSetTag = (tagArr = []) => {
  if (tagArr.length!=0) {
      JPushModule.setTags(tagArr, () => {
          console.log("swx->"+tagArr);
        console.log("Set tag succeed");
      }, () => {
        console.log("Set tag failed");
      });
  }
};

/**
 * 清空Tag
 * @constructor
 */
export const JPushCleanTags = (tagArr = []) => {
    JPushModule.cleanTags( () => {
        console.log("Clean tag succeed");
    }, () => {
        console.log("Clean tag failed");
    });
};

/**
 * 设置别名
 * @constructor
 */
export const JPushSetAlias = (alias) => {
  if (alias) {

    JPushModule.setAlias(alias, () => {
        console.log("swx->"+alias);
      console.log("JPush Set alias succeed");
    }, () => {
      console.log("JPush Set alias failed");
    });
  }
};

/**
 * 删除原有别名
 * @constructor
 */
export const JPushDeleteAlias = () => {
    JPushModule.deleteAlias(() => {
        console.log("JPush Clean alias succeed");
    }, () => {
        console.log("JPush Clean alias failed");
    });
};

/**
 * 清除通知
 * @constructor
 */
export const JPushClear = () => {
  JPushModule.clearAllNotifications();
};